/*
 * Powered By xTOOLs
 * Dev by DuRisk@pactera.com
 * Copyright © 2009 - 2017 pactera.com. All rights reserved.
 *
 * Generated at 2017年08月07日 14:28:58
 *
 */

package com.pactera.jep.service.sys.web.controller;

import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.model.StaffProxyLog;
import com.pactera.jep.sys.service.StaffProxyLogService;
import com.pactera.jep.sys.service.StaffService;
import com.pactera.jep.sys.service.UnitService;
import com.pactera.jep.web.code.WebCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "用户")
@RestController
@RequestMapping("/staffProxyLog")
public class StaffProxyLogRestController extends BaseRestController<StaffProxyLog> {
	Logger logger = LoggerFactory.getLogger(StaffProxyLogRestController.class);

	@Autowired
	private StaffProxyLogService	staffProxyLogService;
	
	@Autowired
	private StaffService			staffService;
	
	@Autowired
	private UnitService				unitService;

	/**
	 * 为列表页面做数据准备
	 */
	@Override
	protected void prepareQuery(String token,Page<StaffProxyLog> page, List<Filter> filters, List<Order> orders,HttpServletRequest request) {
		
		staffProxyLogService.query(page, filters, orders);
	}
	
	/**
	 * 用户借调部门弹出框
	 * @param deptCode 部门编码
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/proxyLogDlg")
	public String proxyLogDlg(String deptCode, Model model) {
		
		model.addAttribute("deptCode", deptCode);
		
		return "sys/staffProxyLogDlg";
	}
	
	/**
	 * 保存
	 * @param staffProxyLog
	 * @return
	 */
	@ApiOperation(value = "保存借调")
	@PostMapping(value = {"/save"})
	public Map<String, Object> save(@RequestBody StaffProxyLog staffProxyLog) {
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		try 
		{
			staffProxyLogService.saveProxyLog(staffProxyLog);
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		return tips;
	}
	
    /**
     * 保存
     * @param staffProxyLog
     * @return
     */
    @ApiOperation(value = "取消借调")
    @DeleteMapping (value = {"/remove/{proxyStaffCode}"})
	public Map<String, Object> remove(@PathVariable String proxyStaffCode){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		try 
		{
			List<Filter> filters = new ArrayList<Filter>();
			filters.add(new Filter("proxyStaffCode", proxyStaffCode));
			List<StaffProxyLog> proxyList = staffProxyLogService.select(filters);
			
			staffProxyLogService.remove(proxyList.get(0));
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
		} 
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
		}
		
		return tips;
	}
}